package bean;


import java.io.Serializable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;

import metier.Classe;


@ManagedBean
public class Utilisateur implements Serializable {
    
    @ManagedProperty(value="#{Connexion}")
    private Connexion connexion=new Connexion();
    private Classe classe;
    private String nom;
    private String prenom;
    private String photoDeProfil;

    public Connexion getConnexion() {
        return connexion;
    }

    public void setConnexion(Connexion connexion) {
        this.connexion = connexion;
    }
    private String born;
    private String pwd;
    private String email;
    private String photo;
    private ArrayList <String> urlphotos = new ArrayList <String>();
    private ArrayList <String> urlphotosVue = new ArrayList <String>();
    private ArrayList <String> friendList = new ArrayList <String>();
    private ArrayList <String> friendListVue = new ArrayList <String>();
    private ArrayList <String> searchList = new ArrayList <String>();
    private ArrayList <String> msgList = new ArrayList <String>();
    private ArrayList <String> msgListVue = new ArrayList <String>();

    public ArrayList<String> getMsgListVue() {
        return msgListVue;
    }

    public void setMsgListVue(ArrayList<String> msgListVue) {
        this.msgListVue = msgListVue;
    }
    private String emailEmetteur;

    public String getEmailEmetteur() {
        return emailEmetteur;
    }

    public void setEmailEmetteur(String emailEmetteur) {
        this.emailEmetteur = emailEmetteur;
    }

    public ArrayList<String> getMsgList() {
        return msgList;
    }

    public void setMsgList(ArrayList<String> msgList) {
        this.msgList = msgList;
    }
    private boolean profile;
    private String nomRecherche;
    private String nomVue;
    private String prenomVue;
    private String bornVue;
    private String pwdVue;
    private String emailVue;
    private String photoDeProfilVue;
    private String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
    
    public String getPhotoDeProfilVue() {
        return photoDeProfilVue;
    }

    public void setPhotoDeProfilVue(String photoDeProfilVue) {
        this.photoDeProfilVue = photoDeProfilVue;
    }

    public ArrayList<String> getUrlphotosVue() {
        return urlphotosVue;
    }

    public void setUrlphotosVue(ArrayList<String> urlphotosVue) {
        this.urlphotosVue = urlphotosVue;
    }

    public String getBornVue() {
        return bornVue;
    }

    public void setBornVue(String bornVue) {
        this.bornVue = bornVue;
    }

    public String getPwdVue() {
        return pwdVue;
    }

    public void setPwdVue(String pwdVue) {
        this.pwdVue = pwdVue;
    }
   
    public ArrayList<String> getFriendListVue() {
        return friendListVue;
    }

    public void setFriendListVue(ArrayList<String> friendListVue) {
        this.friendListVue = friendListVue;
    }

    public String getEmailVue() {
        return emailVue;
    }

    public void setEmailVue(String emailVue) {
        this.emailVue = emailVue;
    }

    public String getPhotoDeProfil() {
        return photoDeProfil;
    }

    public void setPhotoDeProfil(String photoDeProfil) {
        this.photoDeProfil = photoDeProfil;
    }

    public String getNomVue() {
        return nomVue;
    }

    public void setNomVue(String nomVue) {
        this.nomVue = nomVue;
    }

    public String getPrenomVue() {
        return prenomVue;
    }

    public void setPrenomVue(String prenomVue) {
        this.prenomVue = prenomVue;
    }

    public ArrayList<String> getSearchList() {
        return searchList;
    }

    public void setSearchList(ArrayList<String> searchList) {
        this.searchList = searchList;
    }

    public String getNomRecherche() {
        return nomRecherche;
    }

    public void setNomRecherche(String nomRecherche) {
        this.nomRecherche = nomRecherche;
    }

    public boolean isProfile() {
        return profile;
    }

    public void setProfile(boolean profile) {
        this.profile = profile;
    }

    public Utilisateur(String nom, String prenom) throws SQLException, ClassNotFoundException {
        this.nom = nom;
        this.prenom = prenom;

    }
    
    public ArrayList<String> getFriendList() {
        return friendList;
    }

    public void setFriendList(ArrayList<String> friendList) {
        this.friendList = friendList;
    }

    public String getPhoto() {
        return photo;
    }

    public void setPhoto(String photo) {
        this.photo = photo;
    }
    

    public ArrayList<String> getUrlphotos() {
        return urlphotos;
    }

    public void setUrlphotos(ArrayList<String> urlphotos) {
        this.urlphotos = urlphotos;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
 
 
    public Utilisateur() throws SQLException, ClassNotFoundException {
    
    }

    public String getBorn() {
        return born;
    }

    public String getPhotosDeProfil() {
        return photoDeProfil;
    }

    public void setBorn(String born) {
        this.born = born;
    }

    public void setPhotosDeProfil(String photosDeProfil) {
        this.photoDeProfil = photosDeProfil;
    }

    public void setPhotos(String photosDeProfil) {
        this.photoDeProfil = photosDeProfil;
    }

    public String getPhotos() {
        return photoDeProfil;
    }
    
    
    
    public String getPrenom() {
        return prenom;
    }
    
    public void setPrenom(String prenom) {
        this.prenom = prenom;
    }

    public String getNom() {
        return nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

public boolean creerUtilisateur() throws SQLException, ClassNotFoundException {

if(connexion.getConnec()!=null){
    
    email=email.toUpperCase();
    nom=nom.toUpperCase();
    prenom=prenom.toUpperCase();
    pwd=this.pwd.toUpperCase();
    photoDeProfil="";

    Statement requete=connexion.getConnec().createStatement();
    requete.execute("INSERT INTO utilisateur (email,nom,prenom,mdp,datenaissance) VALUES ('"+email+"','"+nom+"','"+prenom+"','"+pwd+"','"+born+"');");
    return true;
}

else {
    return false;
}
		
}

public void ajouterPhoto() throws ClassNotFoundException, SQLException{
    
    
    email=this.email.toUpperCase();
    String nom=this.nom;
    String prenom=this.prenom;
    String born=this.born;
    String pwd=this.pwd;
    boolean selected=this.profile;
    urlphotos.add(this.photo);

if(connexion!=null){
    Statement requete=connexion.getConnec().createStatement();
    String SQL="INSERT INTO photos VALUES ('"+email+"','"+this.photo+"');";
    requete.execute(SQL);
    requete.close();
    if(selected==true){
        
        Statement requete2=connexion.getConnec().createStatement();
        String SQL2="UPDATE utilisateur SET photoprofil='"+this.photo+"' WHERE email='"+email+"';";
        requete2.execute(SQL2);
        requete2.close();
        
        Statement requete3=connexion.getConnec().createStatement();
        String SQL3="SELECT * FROM utilisateur WHERE email='"+email+"';";
        ResultSet rs=requete3.executeQuery(SQL3);
        
        while(rs.next()){
       
        setPhotosDeProfil(rs.getString("photoprofil"));
        
         }
    }
}

else {
    
    System.err.println("CAILLE SQL");
    
}   
}

public String connexion() throws SQLException, ClassNotFoundException{
msgList.clear();
if(connexion.getConnec()!=null){
    
    String email=this.email.toUpperCase();
    String pwd=this.pwd.toUpperCase();
       
    friendList.clear();
    urlphotos.clear();
    friendListVue.clear();
    urlphotosVue.clear();
    
    Statement requete=connexion.getConnec().createStatement();
    String SQL="SELECT * FROM utilisateur WHERE email='"+email+"' AND mdp='"+pwd+"';";
    ResultSet rs = requete.executeQuery(SQL);
    
    if(rs.next()){
        
        setNom(rs.getString("nom"));
        setPrenom(rs.getString("prenom"));
        setPwd(rs.getString("mdp"));
        setEmail(rs.getString("email"));
        setBorn(rs.getString("datenaissance"));
        setPhotosDeProfil(rs.getString("photoprofil"));
        
       Statement requete2=connexion.getConnec().createStatement();
       String SQL2="SELECT path FROM photos WHERE email='"+email+"';";
       ResultSet rs2 = requete2.executeQuery(SQL2);
       
       while(rs2.next()){
           
           urlphotos.add(rs2.getString("path"));
           
       }
       
       Statement requete3=connexion.getConnec().createStatement();
       String SQL3="SELECT * FROM utilisateur WHERE email IN (SELECT email2 from amis where email1='"+email+"');";
       ResultSet rs3 = requete3.executeQuery(SQL3);
       while(rs3.next()){
           
           friendList.add(rs3.getString("nom")+""+rs3.getString("prenom"));
           
       }
       
       Statement requete4=connexion.getConnec().createStatement();
       String SQL4="SELECT message, email_emetteur FROM messages WHERE email_destinataire='"+email+"';";
       ResultSet rs4 = requete4.executeQuery(SQL4);
       
       while(rs4.next()){
                
                String from=rs4.getString("email_emetteur").replaceAll(" ","");
                
                String msg=returnAuthor(from)+" : "+rs4.getString("message");
                msgList.add(msg);
                

       }
        requete.close();
        requete2.close();
        return "utilisateurOK";
    }
    else{
        return "utilisateurKO";
    }
    
 
}

   return "KO";
}

public String connexionVue(String entry) throws SQLException, ClassNotFoundException{
    msgListVue.clear();
if(connexion.getConnec()!=null){
   // String test = entry.
    String [] entries = entry.split("                                          ");
    int caca=0;
    nomVue=entries[0].replaceAll(" ", "");
    prenomVue=entries[1].replaceAll(" ", "");
    friendListVue.clear();
    urlphotosVue.clear();
    Statement requete=connexion.getConnec().createStatement();
    String SQL="SELECT * FROM utilisateur WHERE nom='"+nomVue+"' AND prenom='"+prenomVue+"';";
    ResultSet rs = requete.executeQuery(SQL);
    
    while(rs.next()){
       caca=1;
       setPwdVue(rs.getString("mdp"));
       setEmailVue(rs.getString("email"));
       setBornVue(rs.getString("datenaissance"));
       setPhotoDeProfilVue(rs.getString("photoprofil"));
       Statement requete2=connexion.getConnec().createStatement();
       String SQL2="SELECT path FROM photos WHERE email='"+emailVue+"';";
       ResultSet rs2 = requete2.executeQuery(SQL2);
       
       while(rs2.next()){
           urlphotosVue.add(rs2.getString("path"));
       }
       
       Statement requete3=connexion.getConnec().createStatement();
       String SQL3="SELECT * FROM utilisateur WHERE email IN (SELECT email2 from amis where email1='"+emailVue+"');";
       ResultSet rs3 = requete3.executeQuery(SQL3);
       while(rs3.next()){
           
           friendListVue.add(rs3.getString("nom")+""+rs3.getString("prenom"));
           
       }
       
       Statement requete4=connexion.getConnec().createStatement();
       String SQL4="SELECT message, email_emetteur FROM messages WHERE email_destinataire='"+emailVue+"';";
       ResultSet rs4 = requete4.executeQuery(SQL4);
       
       while(rs4.next()){
                
                String from=rs4.getString("email_emetteur").replaceAll(" ","");
                
                String msg=returnAuthor(from)+" : "+rs4.getString("message");
                msgListVue.add(msg);
                

       }
        
       requete.close();
       requete2.close();
       return "success";
    }
    if(caca==0){
        return "utilisateurKO";
    }
    
 
}

   return "KO";
}

public String rechercherAmis() throws ClassNotFoundException, SQLException{
    
    String nomRecherche=this.nomRecherche.toUpperCase();
    
    searchList.clear();
    if(connexion.getConnec()!=null){
        
    Statement requete=connexion.getConnec().createStatement();
    String SQL="SELECT * FROM utilisateur WHERE nom LIKE '%"+nomRecherche+"%' AND nom <> '"+nom+"';";
    ResultSet rs = requete.executeQuery(SQL);
        
    while(rs.next()){
           
           searchList.add(rs.getString("nom")+"/"+rs.getString("prenom")+"/"+rs.getString("email"));
           
       }
    setNomRecherche(null);
    return "rechercheOK";
    }
    else {
        setNomRecherche(null);
        return "rechercheKO";
    }
    
    
}
public void addFriend(String email2) throws SQLException, ClassNotFoundException{
    

    String amis[] =  email2.split("/");
    
    if(connexion.getConnec()!=null){
        
    Statement requete=connexion.getConnec().createStatement();
    String SQL="INSERT INTO amis (email1,email2) VALUES ('"+this.email.toUpperCase()+"','"+amis[2]+"');";
    requete.execute(SQL);
    
    Statement requete2=connexion.getConnec().createStatement();
    String SQL2="SELECT * FROM utilisateur WHERE email='"+amis[2]+"';";
    ResultSet rs=requete2.executeQuery(SQL2);
    while(rs.next()){
        
        friendList.add(rs.getString("nom")+""+rs.getString("prenom"));
    }
    
    }
    
    
    
}

public String renvoieImage(String utilisateur) throws SQLException, ClassNotFoundException{
    
   String [] utilisateurs = utilisateur.split(" ");

    String name=utilisateurs[0];
   // String prename=utilisateurs[1];
    if(connexion.getConnec()!=null){
        
    Statement requete=connexion.getConnec().createStatement();
    String SQL="SELECT photoprofil FROM utilisateur WHERE nom='"+name+"';";
    ResultSet rs= requete.executeQuery(SQL);

    while(rs.next()){
        
        return rs.getString("photoprofil");
        
    }

        return "http://userserve-ak.last.fm/serve/252/58531987.jpg";
  
    }
    else {
        return "KO";
    }
    
}
public String renvoieImageVue(String utilisateur) throws SQLException, ClassNotFoundException{
    
    
    String [] utilisateurs = utilisateur.split(" ");

    String name=utilisateurs[0];
    String prename=utilisateurs[1];
    if(connexion.getConnec()!=null){
        
    Statement requete=connexion.getConnec().createStatement();
    String SQL="SELECT photoprofil FROM utilisateur WHERE nom='"+name+"';";
    ResultSet rs= requete.executeQuery(SQL);

    while(rs.next()){
        
        return rs.getString("photoprofil");
        
    }
    
    
        return "http://userserve-ak.last.fm/serve/252/58531987.jpg";
  
    }
    else {
        return "KO";
    }
    
    
}

 public String deconnexion(){
        
    friendList.clear();
    urlphotos.clear();
    friendListVue.clear();
    urlphotosVue.clear();
    msgList.clear();
    profile=false;
    nomRecherche="";
    nomVue="";
    prenomVue="";
    bornVue="";
    pwdVue="";
    emailVue="";
    photoDeProfilVue="";
    nom="";
    prenom="";
    photoDeProfil="";
    born="";
    pwd="";
    email="";
    photo="";
    
    return "Deco";

        
    }
 
 public String ecrireMessage() throws SQLException{
        
    Statement requete=connexion.getConnec().createStatement();
    String SQL="INSERT INTO messages VALUES ('"+emailVue+"','"+email+"','"+message+"');";
    requete.execute(SQL);
    
    return "addMessage";
     
        
}

public String returnAuthor(String email) throws SQLException{
    
       Statement requete5=connexion.getConnec().createStatement();
       String SQL5="SELECT nom, prenom FROM utilisateur WHERE email='"+email+"';";
       ResultSet rs5 = requete5.executeQuery(SQL5);
    
        while(rs5.next()){
           
           String nomEmetteur=rs5.getString("nom")+""+rs5.getString("prenom");           
           return nomEmetteur;
        }
       
       return "";
}

public String returnAuthorVue (String email) throws SQLException{
    
       Statement requete5=connexion.getConnec().createStatement();
       String SQL5="SELECT nom, prenom FROM utilisateur WHERE email='"+email+"';";
       ResultSet rs5 = requete5.executeQuery(SQL5);
    
        while(rs5.next()){
           
           String nomEmetteur=rs5.getString("nom")+""+rs5.getString("prenom");           
           return nomEmetteur;
        }
       
       return "";
}

}
